package contest.oop.sse;

import contest.oop.sse.key.FileKeyLoaderFactory;
import contest.oop.sse.key.KeyLoader;
import contest.oop.sse.key.KeyLoaderFactory;
import contest.oop.sse.message.MessageType;
import contest.oop.sse.person.Agent;
import contest.oop.sse.person.CpcAgent;
import contest.oop.sse.person.KmtAgent;

/**
 * 主类，描述整个故事
 *
 * @author paule
 */
public class Story {
    public static void main(String[] args) {
        // 基本对象的创建与连接
        Agent cpcAgent1 = new CpcAgent("jwmao");
        Agent cpcAgent2 = new CpcAgent("qrchen");
        Agent kmtAgent = new KmtAgent("tewu");
        KeyLoaderFactory factory = new FileKeyLoaderFactory();
        KeyLoader loader = factory.createKeyLoaderFactory("c:/users/paule/cpc.key");
        cpcAgent1.addConnector(cpcAgent2);
        cpcAgent1.addConnector(kmtAgent);
        cpcAgent2.addConnector(cpcAgent1);
        kmtAgent.addConnector(cpcAgent1);

        // cpcAgent1 ---- clientHello ----> cpcAgent2
        ((CpcAgent) cpcAgent2).getPublicKey(loader, "cpc");
        cpcAgent1.createMessage(MessageType.CLIENT_HELLO, "通信开始");
        cpcAgent1.send();


        // cpcAgent2 ---- serverHello ----> cpcAgent1
        cpcAgent2.createMessage(MessageType.SERVER_HELLO, "已收到请求");
        cpcAgent2.send();
        ((CpcAgent) cpcAgent2).getPrivateKey(loader, "cpc");

        // cpcAgent1 <---- handshake ----> cpcAgent2
        cpcAgent1.createMessage(MessageType.HANDSHAKE);
        cpcAgent1.send();
        cpcAgent2.createMessage(MessageType.HANDSHAKE);
        cpcAgent2.send();

        // cpcAgent1 ---- standard ----> cpcAgent2
        cpcAgent1.createMessage(MessageType.STANDARD, "Secret Information");
        cpcAgent1.send();
        cpcAgent2.createMessage(MessageType.STANDARD, "Receive your information");
        cpcAgent2.send();

        System.out.println("秘密信道建立完成");
    }
}
